package com.nep.NEPM.controller;

import com.nep.entity.administrator.User;
import com.nep.common.util.AlertUtil;
import com.nep.common.util.SceneSwitcherUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

import java.util.logging.Level;
import java.util.logging.Logger;

public class SupervisorViewController {
    private static final Logger LOGGER = Logger.getLogger(SupervisorViewController.class.getName());

    @FXML
    private Label lblId;          // 编号
    @FXML
    private Label lblName;        // 姓名
    @FXML
    private Label lblProvince;    // 省份
    @FXML
    private Label lblCity;        // 城市
    @FXML
    private Label lblGrade;       // 污染等级
    @FXML
    private Label lblDate;        // 日期
    @FXML
    private Label lblTime;        // 时间
    @FXML
    private Label lblPhone;
    @FXML
    private Label lblAddress;
    @FXML
    private Label lblDescription;
    @FXML
    private Label lblSex;
    @FXML
    private Label lblBornDate;
    @FXML
    private Label lblDegree;

    // 初始化视图数据的方法
    public void initData(User user) {
        setLabelText(lblPhone, user.getPhone());
        setLabelText(lblAddress, user.getAddress());
        setLabelText(lblDescription, user.getDescription());
        setLabelText(lblSex, user.getSex());
        setLabelText(lblBornDate, user.getBornDate());
        setLabelText(lblDegree, user.getDegree());
        setLabelText(lblId, user.getId());
        setLabelText(lblName, user.getName());
        setLabelText(lblProvince, user.getProvince());
        setLabelText(lblCity, user.getCity());
        setLabelText(lblGrade, user.getGrade());
        setLabelText(lblDate, user.getDate());
        setLabelText(lblTime, user.getTime());
    }

    // 封装设置标签文本的方法
    private void setLabelText(Label label, String text) {
        if (label != null) {
            label.setText(text);
        }
    }

    @FXML
    public void OUT(ActionEvent actionEvent) {
        switchScene(actionEvent, "/com/nep/NEPM/view/IndexView.fxml");
    }

    @FXML
    public void Back(ActionEvent actionEvent) {
        switchScene(actionEvent, "/com/nep/NEPM/view/SupervisorListView.fxml");
    }

    // 封装场景切换方法
    private void switchScene(ActionEvent actionEvent, String fxmlPath) {
        try {
            SceneSwitcherUtil.switchScene((javafx.scene.Node) actionEvent.getSource(), fxmlPath);
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "场景切换失败", e);
            AlertUtil.showError("系统错误", "场景切换失败: " + e.getMessage());
        }
    }
}